import { EventKey } from "../common/Key";
cc.Class({
    extends: require('BaseUI'),

    properties: {
        nodeBg: cc.Node,
    },

    onLoad() {
        this._super();
        G.game.setScene(this);
        this._initCanvasFit();
        this._initLayers();
        cc.debug.setDisplayStats(G.config.SHOWDEBUG);
    },

    // 屏幕适配
    _initCanvasFit() {
        const canvas = this.node.getComponent(cc.Canvas);
        let designScale = canvas.designResolution.width / canvas.designResolution.height;
        if (designScale <= cc.view.getCanvasSize().width / cc.view.getCanvasSize().height) {
            canvas.fitHeight = true;
            canvas.fitWidth = false;
            if (!this.nodeBg) {
                return;
            }
            let scale = canvas.designResolution.height / cc.view.getCanvasSize().height;
            this.nodeBg.width = scale * cc.view.getCanvasSize().width;
            this.nodeBg.height = this.nodeBg.width / designScale;
        } else {
            canvas.fitHeight = false;
            canvas.fitWidth = true;
            if (!this.nodeBg) {
                return;
            }
            let scale = canvas.designResolution.width / cc.view.getCanvasSize().width;
            this.nodeBg.height = scale * cc.view.getCanvasSize().height;
            this.nodeBg.width = this.nodeBg.height * designScale;
        }
    },

    // 添加
    _initLayers() {
        this.layerPanel = new cc.Node('layerPanel');
        this.layerPanel.parent = this.node;
        this.layerPanel.zIndex = 100;
        let widget = this.layerPanel.addComponent(cc.Widget);
        widget.isAlignTop = true;
        widget.isAlignBottom = true;
        widget.isAlignLeft = true;
        widget.isAlignRight = true;
        widget.top = 0;
        widget.bottom = 0;
        widget.left = 0;
        widget.right = 0;

        this.layerToast = new cc.Node('layerToast');
        this.layerToast.parent = this.node;
        this.layerToast.zIndex = 101;

        this.layerBlockInput = new cc.Node('layerToast');
        this.layerBlockInput.parent = this.node;
        this.layerBlockInput.zIndex = 102;
        widget = this.layerBlockInput.addComponent(cc.Widget);
        widget.isAlignTop = true;
        widget.isAlignBottom = true;
        widget.isAlignLeft = true;
        widget.isAlignRight = true;
        widget.top = 0;
        widget.bottom = 0;
        widget.left = 0;
        widget.right = 0;
        this.layerBlockInput.addComponent(cc.BlockInputEvents);
        this.layerBlockInput.active = false;
    },

    onEnable() {
        G.onFire.on(EventKey.EVENT_BLOCK_INPUT, this._blockInput, this);
        G.onFire.on(EventKey.EVENT_CANCEL_BLOCK_INPUT, this._cancelBlockInput, this);
    },
    onDisable() {
        G.onFire.off(EventKey.EVENT_BLOCK_INPUT, this._blockInput, this);
        G.onFire.off(EventKey.EVENT_CANCEL_BLOCK_INPUT, this._cancelBlockInput, this);
    },

    _blockInput() {
        this.layerBlockInput.active = true;
    },
    _cancelBlockInput() {
        this.layerBlockInput.active = false;
    },

    toast(content, delay) {
        let toast = G.utils.getPoolNode('nodePoolToast');
        if (toast) {
            toast.parent = this.layerToast;
            toast.getComponent('ItemToast').init(content, delay);
        }
    },
});
